/**
Copyright (c) 2008 New Gonzo <john@newgonzo.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/


package com.newgonzo.cannonball.browser.rendering
{
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;
	
	import com.newgonzo.commons.logging.*;
	
	import com.newgonzo.cannonball.browser.rendering.IRenderer
	
	public class AbstractDisplayTemplate extends AbstractDisplayRenderer
	{
		protected static const log:ILogger = Log.getLogger(AbstractDisplayTemplate);
		
		private var allRendererNames:Array;
		
		private var dynamicRenderers:Dictionary = new Dictionary();
		private var dynamicRendererNames:Dictionary = new Dictionary();
		
		public function AbstractDisplayTemplate(dynamicRendererNames:Array = null)
		{
			allRendererNames = dynamicRendererNames;
		}
		
		public function get rendererNames():Array
		{
			if(allRendererNames)
			{
				return allRendererNames;
			}
			
			return new Array();
		}

		public function setRenderer(rendererName:String, renderer:IRenderer):void
		{
			log.debug("AbstractDisplayTemplate.setRenderer(" + arguments + ")");
			
			if(dynamicRenderers[rendererName])
			{
				hideRenderer(rendererName);
			}
			
			addRenderer(rendererName, renderer);
		}
		
		public function getRenderer(rendererName:String):IRenderer
		{
			log.debug("AbstractDisplayTemplate.getRenderer(" + arguments + ")");
			
			if(dynamicRenderers[rendererName])
			{
				return dynamicRenderers[rendererName] as IRenderer;
			}
			
			return getStaticRenderer(rendererName);
		}
		
		protected function getRendererName(renderer:IRenderer):String
		{
			return dynamicRendererNames[renderer] as String;
		}
		
		protected function getStaticRenderer(rendererName:String):IRenderer
		{
			return getChildByName(rendererName) as IRenderer;
		}
		
		protected function getNamedContainer(rendererName:String):DisplayObjectContainer
		{
			return getChildByName(rendererName) as DisplayObjectContainer;
		}
		
		protected function hasRenderer(rendererName:String):Boolean
		{
			return dynamicRenderers[rendererName] is IRenderer;
		}
		
		protected function addRenderer(rendererName:String, renderer:IRenderer):void
		{
			dynamicRenderers[rendererName] = renderer;
			dynamicRendererNames[renderer] = rendererName;
			
			getNamedContainer(rendererName).addChild(renderer as DisplayObject);
			showRenderer(rendererName);
		}
			
		protected function removeRenderer(rendererName:String):void
		{
			var renderer:IRenderer = getRenderer(rendererName);
			
			delete dynamicRenderers[rendererName];
			delete dynamicRendererNames[renderer];
			
			getNamedContainer(rendererName).removeChild(renderer as DisplayObject);
		}
		
		protected function showRenderer(rendererName:String):void
		{
			
		}
		
		protected function hideRenderer(rendererName:String):void
		{
			removeRenderer(rendererName);
		}
	}
}
